
#include "ball_query_tiling.h"
#include "register/op_def_registry.h"
#include <cstdio>


namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    BallQueryTilingData tiling;
    // 获取属性
    const float *min_radius = context->GetAttrs()->GetFloat(0);
    const float *max_radius = context->GetAttrs()->GetFloat(1);
    const int64_t *sample_num = context->GetAttrs()->GetInt(2);
    // 设置属性
    tiling.set_min_radius(*min_radius);
    tiling.set_max_radius(*max_radius);
    tiling.set_sample_num((int32_t)*sample_num);

    /*
    输入shape：xyz: (B, N, 3); center_xyz: (B, M, 3)
    输出shape：(B, M, sample_num)
    b = 5, n = 15, m = 20, sample_num = 2
    */
    auto shape_xyz = context->GetInputShape(0)->GetOriginShape();
    auto shape_center_xyz = context->GetInputShape(1)->GetOriginShape();
    // int dimNum = shape_xyz.GetDimNum();
    // printf("dimNum: %d\n", dimNum);
    // for (int i = 0; i < dimNum; i++) {
    //     printf("shape_xyz: %d\n", shape_xyz.GetDim(i));
    // }
    // dimNum = shape_center_xyz.GetDimNum();
    // printf("dimNum: %d\n", dimNum);
    // for (int i = 0; i < dimNum; i++) {
    //     printf("shape_center_xyz: %d\n", shape_center_xyz.GetDim(i));
    // }

    if(shape_xyz.GetDimNum() == 3) {
        // printf("shape_xyz dim: 3\n");
        int B = shape_xyz.GetDim(0);
        int N = shape_xyz.GetDim(1);
        int M = shape_center_xyz.GetDim(1);
        tiling.set_B(B);
        tiling.set_N(N);
        tiling.set_M(M);
    }else {
        // printf("shape_xyz dim: 2\n");
        auto shape_xyz_batch_cnt = context->GetInputShape(2)->GetOriginShape();
        auto shape_center_xyz_batch_cnt = context->GetInputShape(3)->GetOriginShape();
        int B = shape_xyz_batch_cnt.GetDim(0);
        int N = shape_xyz.GetDim(0);
        int M = shape_center_xyz.GetDim(0);
        tiling.set_B(B);
        tiling.set_N(N);
        tiling.set_M(M);
    }
    // printf("b: %d, n: %d, m: %d, sample_num: %d\n", B, N, M, tiling.get_sample_num());

    //设置切分策略
    uint32_t totalLength = context->GetInputShape(0)->GetOriginShape().GetShapeSize();
    tiling.set_totalLength(totalLength);
    // printf("totalLength: %d\n", totalLength);
    tiling.set_tileNum(1);
    // 判断是否有可选输入
    auto optional_tensor = context->GetOptionalInputTensor(2);
    if (optional_tensor == nullptr) {
        // 可选输入没有实例化
        context->SetTilingKey(1);
    } else {
        // 可选输入实例化
        context->SetTilingKey(2);
    }
    context->SetBlockDim(1);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class BallQuery : public OpDef {
public:
    explicit BallQuery(const char* name) : OpDef(name)
    {
        this->Input("xyz")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("center_xyz")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("xyz_batch_cnt")
            .ParamType(OPTIONAL)
            .DataType({ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("center_xyz_batch_cnt")
            .ParamType(OPTIONAL)
            .DataType({ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("idx")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("min_radius").Float();
        this->Attr("max_radius").Float();
        this->Attr("sample_num").Int();

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");

    }
};

OP_ADD(BallQuery);
}
